package com.wsmhz.security.core.social.qq.api.impl;

import com.wsmhz.security.core.social.qq.api.QQ;
import com.wsmhz.security.core.social.qq.api.QQUserInfo;
import com.wsmhz.security.core.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.TokenStrategy;

/**
 * create by tangbj on 2018/4/3
 */
public class QQImpl extends AbstractOAuth2ApiBinding implements QQ {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private static  final String URL_GET_OPENID = "https://graph.qq.com/oauth2.0/me?access_token=%s";
    private static  final String URL_GET_USERINFO = "https://graph.qq.com/user/get_user_info?oauth_consumer_key=%s&openid=%s";
    private String appId;
    private String openId;

    public QQImpl(String accessToken,String appId){
        super(accessToken, TokenStrategy.ACCESS_TOKEN_PARAMETER);
        this.appId = appId;
        String url = String.format(URL_GET_OPENID,accessToken);
        String result = getRestTemplate().getForObject(url,String.class);
        logger.info("获取openId结果:",result);
        this.openId = StringUtils.substringBetween(result,"\"openid\":\"","\"}");
    }

    @Override
    public QQUserInfo getUserInfo() {
        String url = String.format(URL_GET_USERINFO,appId,openId);
        String result = getRestTemplate().getForObject(url,String.class);
        logger.info("获取UserInfo结果:",result);
        QQUserInfo qqUserInfo = JsonUtil.stringToObj(result,QQUserInfo.class);
        qqUserInfo.setOpenId(openId);
        return qqUserInfo;
    }
}
